package com.rchat.pocmini.activity.view.location;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by 59450 on 2017/3/10.
 */

public class CommonViewHolder {
    private final SparseArray<View> mViews;
    private View mConvertView;
    private int mPosition;

    private CommonViewHolder(Context context, ViewGroup parent, int layoutId, int position) {
        this.mViews = new SparseArray<View>();
//		this.mPosition = position;
        mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
        // setTag
        mConvertView.setTag(this);

    }

    /**
     * 拿到一个ViewHolder对象
     *
     * @param context
     * @param convertView
     * @param parent
     * @param layoutId
     * @param position
     * @return
     */
    public static CommonViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId, int position) {

        if (convertView == null) {
            return new CommonViewHolder(context, parent, layoutId, position);
        }
        return (CommonViewHolder) convertView.getTag();
    }

    /**
     * 通过控件的Id获取对于的控件，如果没有则加入views
     *
     * @param viewId
     * @return
     */
    public <T extends View> T getView(int viewId) {

        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    public View getConvertView() {
        return mConvertView;
    }
    /**
     * gridview实验好像不准确，
     * @return
     */
//	public int getPosition() {
//		return mPosition;
//	}

    public CommonViewHolder setVisible(int viewId, int visible) {
        getView(viewId).setVisibility(visible);
        return this;
    }

    /**
     * 为TextView设置字符串
     */
    public TextView setText(int viewId, CharSequence text) {
        TextView textView = getView(viewId);
        textView.setText(text);
        return textView;
    }

    /**
     * 为ImageView设置图片
     */
    public ImageView setImageResource(int viewId, int drawableId) {
        ImageView imageView = getView(viewId);
        imageView.setImageResource(drawableId);
        return imageView;
    }

    public ImageView setImageBitmap(int viewId, Bitmap bm) {
        ImageView imageView = getView(viewId);
        imageView.setImageBitmap(bm);
        return imageView;
    }
//
//	public ImageView setImageByUrl(int viewId, String url, DisplayImageOptions options,
//			ImageLoadingListener loadingListener) {
//		ImageView imageView = getView(viewId);
//		ImageLoader.getInstance().displayImage(url, imageView, options, loadingListener);
//		return imageView;
//	}
//
//	public ImageView setImageByUrl(int viewId, String url, DisplayImageOptions options) {
//		return setImageByUrl(viewId, url, options, null);
//	}
//
//	public ImageView setImageByUrl(int viewId, String url) {
//		return setImageByUrl(viewId, url, MFUtil.getImageLoaderOptions(), null);
//	}
}

